/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-29
*/
package com.rzico.order.service;

import com.rzico.account.entity.Refunds;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Product;
import com.rzico.basics.mapper.ProductMapper;
import com.rzico.exception.CustomException;
import com.rzico.order.entity.*;
import com.rzico.order.mapper.BargainOrderMapper;
import com.rzico.order.mapper.BargainPeriodMapper;
import com.rzico.order.mapper.BargainProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.order.mapper.BargainBuyMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <pre>
 * 砍价活动业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class BargainBuyService extends BaseServiceImpl<BargainBuy, String> {

    @Autowired
    private BargainBuyMapper bargainBuyMapper;

    @Autowired
    private BargainProductMapper bargainProductMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public BaseMapper<BargainBuy, String> getMapper() {
        return bargainBuyMapper;
    }


    @Transactional(rollbackFor = Exception.class)
    public Integer save(BargainBuy bargainBuy) {

        Integer w = bargainBuyMapper.insertUseGeneratedKeys(bargainBuy);
        for (BargainProduct bargainProduct:bargainBuy.getItemList()) {
            bargainProduct.setCreateDate(new Date());
            bargainProduct.setBargainBuyId(bargainBuy.getId());
            Product product = productMapper.selectByPrimaryKey(bargainProduct.getProductId());
            bargainProduct.setName(product.getName());
            bargainProduct.setSpec(product.getSpec());
            bargainProduct.setUnit(product.getUnit());
            bargainProduct.setThumbnail(product.getThumbnail());
            bargainProductMapper.insertUseGeneratedKeys(bargainProduct);
        }

        return w;

    }

    @Transactional(rollbackFor = Exception.class)
    public Integer update(BargainBuy bargainBuy) {

        Integer w = bargainBuyMapper.updateByPrimaryKeySelective(bargainBuy);

        BargainProduct deleteProduct = new BargainProduct();
        deleteProduct.setBargainBuyId(bargainBuy.getId());
        bargainProductMapper.delete(deleteProduct);

        for (BargainProduct groupProduct:bargainBuy.getItemList()) {
            groupProduct.setCreateDate(new Date());
            groupProduct.setBargainBuyId(bargainBuy.getId());
            Product product = productMapper.selectByPrimaryKey(groupProduct.getProductId());
            groupProduct.setName(product.getName());
            groupProduct.setSpec(product.getSpec());
            groupProduct.setUnit(product.getUnit());
            groupProduct.setThumbnail(product.getThumbnail());
            bargainProductMapper.insertUseGeneratedKeys(groupProduct);
        }

        return w;
    }


    @Transactional(rollbackFor = Exception.class)
    public Integer audit(BargainBuy bargainBuy) {

        if (bargainBuy.getRobotNum()>bargainBuy.getBargainNum()) {
            throw new CustomException("机器人太多");
        }

        bargainBuy.setStatus(1);
        bargainBuyMapper.updateByPrimaryKeySelective(bargainBuy);


        return 1;
    }


    @Transactional(rollbackFor = Exception.class)
    public Integer unAudit(BargainBuy bargainBuy) {


        bargainBuy.setStatus(0);
        bargainBuyMapper.updateByPrimaryKeySelective(bargainBuy);

        return 1;
    }


    @Transactional(rollbackFor = Exception.class)
    public List<Refunds> cancel(BargainBuy bargainBuy) {

        bargainBuy.setStatus(3);
        bargainBuyMapper.updateByPrimaryKeySelective(bargainBuy);
        List<Refunds> refundsList = new ArrayList<>();

//        GroupPeriod groupPeriod = groupPeriodMapper.selectByPrimaryKey(groupBuy.getPeriodId());
//        String sn = "GP"+String.valueOf(groupPeriod.getId());
//
//        Map<String,Object> params = new HashMap<>();
//        params.put("periodId",0L);
//        params.put("robot",0);
//        params.put("groupBuyId",groupBuy.getId());
//        List<GroupOrder> groupOrderList = groupOrderService.selectList(params);
//        if (groupPeriod.getStatus().equals(0)) {
//            //没有完成的订单，做退款处理，不返利
//            List<GroupOrder> periodOrderList = groupOrderService.getOrderList(groupBuy.getPeriodId());
//            for (GroupOrder groupOrder:periodOrderList) {
//                groupOrderList.add(groupOrder);
//            }
//        }
//        //开始处理订单
//        Integer w = 0;
//        for (GroupOrder groupOrder:groupOrderList) {
//            groupOrder.setOrderStatus(2);
//            groupOrderService.updateByPrimaryKeySelective(groupOrder);
//            w++;
//            List<Refunds> refunds = orderService.cancel(groupOrder.getOrderId(), sn + "-" + String.valueOf(w));
//            refundsList.addAll(0,refunds);
//        }

        return refundsList;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            BargainBuy bargainBuy = selectByPrimaryKey(id);
            if (!bargainBuy.getStatus().equals(0)) {
                throw new CustomException("已审核不能删除");
            }

            if (bargainBuy.getPeriodNum()>0) {
                throw new CustomException("已进行不能删除");
            }

//            GroupPeriod groupPeriod = new GroupPeriod();
//            groupPeriod.setGroupBuyId(groupBuy.getId());
//            groupPeriodMapper.delete(groupPeriod);
            rw = rw + super.delete(bargainBuy);
        }
        return rw;
    }

}
